package com.bfb;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.bfb.adapters.DishTypeGridAdapter;
import com.bfb.helpers.DishTypes;

/**
 * This is the {@link Activity} that handles the view for selecting the dish
 * type before playing the game. The content layout for the activity is in the
 * xml layout file dish_types_gridview.xml. It implements an
 * {@link OnItemClickListener} that will handle the event of choosing a dish
 * type from the grid.
 * 
 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
 * 
 */
public class DishTypeActivity extends Activity implements OnItemClickListener {

	/** The list containing the dish type names. */
	public static String[] dishTypeNames = null;

	/** The list containing the dish type drawables. */
	public static TypedArray dishTypeDrawables = null;

	/**
	 * Overridden onCreate method. This is where the contentView is set and all
	 * the views are initialized.
	 * 
	 * @param savedInstanceState
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dish_types_gridview);

		if (dishTypeDrawables == null) {
			dishTypeDrawables = getResources().obtainTypedArray(
					R.array.DishTypeDrawables);
		}

		if (dishTypeNames == null) {
			dishTypeNames = getResources().getStringArray(
					R.array.DishTypeNames2);
		}

		String[] dishTypeNamesLocalized = getResources().getStringArray(
				R.array.DishTypeNames1);

		GridView grid = (GridView) findViewById(R.id.dish_types_grid);
		grid.setAdapter(new DishTypeGridAdapter(dishTypeNamesLocalized,
				dishTypeDrawables, getBaseContext()));
		grid.setOnItemClickListener(this);
	}

	/**
	 * This is the {@link OnItemClickListener} for the {@link GridView} of the
	 * dish types. It will get the dish type name based on the position clicked
	 * and then execute and {@link AsyncTask} called {@link DishTypeAsyncTask}
	 * to get the dish type key from the database to be passed to the next
	 * activity. This activity will be the {@link GameActivity} that will start
	 * the play.
	 * 
	 * @param parent
	 * @param v
	 * @param position
	 * @param id
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		String dishTypeName = dishTypeNames[position];
		String dishTypeKey = "";

		try {
			dishTypeKey = new DishTypeAsyncTask().execute(dishTypeName).get();
		} catch (Exception e) {
			Toast.makeText(this, getString(R.string.error_getting_dishtype),
					Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}

		Intent gameIntent = new Intent(getBaseContext(), GameActivity.class);
		gameIntent.putExtra("dishTypeName", dishTypeName);
		gameIntent.putExtra("dishTypeKey", dishTypeKey);
		gameIntent.putExtra("ingredients", "");
		gameIntent.putExtra("customize", false);
		startActivity(gameIntent);
		finish();

	}

	/**
	 * This is the {@link AsyncTask} task that retrieves the dish type key from
	 * the server. It takes an input the name and returns a string result with
	 * the key. It runs in the background and shows a {@link ProgressDialog}
	 * while the key is being retrieved.
	 * 
	 * @author Rawan Khalil(rawan.tkhalil@gmail.com)
	 * 
	 */
	class DishTypeAsyncTask extends AsyncTask<String, Void, String> {

		private ProgressDialog pd;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pd = ProgressDialog.show(DishTypeActivity.this, "",
					getString(R.string.processing_dishtype), true);
		}

		@Override
		protected String doInBackground(String... params) {
			return DishTypes.getKeyByName(params[0]);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			pd.dismiss();
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			pd.dismiss();
		}

	}
}